<?php
namespace backend\models\course;
use backend\models\course\CourseType;
use backend\models\course\CourseBank;
use backend\models\course\CourseSource;
use backend\models\course\CourseCategory;
use backend\models\Teacher;
use backend\models\course\Course;
use yii\data\Pagination;
use Yii;

/**
 * This is the model class for table "{{%edu_course}}".
 *
 * @property integer $course_id
 * @property string $course_name
 * @property string $course_thumb
 * @property integer $teacher_id
 * @property integer $price_status
 * @property string $course_price
 * @property integer $apply_num
 * @property integer $recruit_num
 * @property integer $type
 * @property integer $source_id
 * @property integer $teacher_from_id
 * @property integer $rank_id
 * @property integer $cate_id
 * @property string $describe
 * @property string $fit_crowd
 * @property string $result
 * @property integer $start_time
 * @property integer $end_time
 * @property integer $status
 * @property integer $add_time
 * @property integer $upd_time
 * @property integer $weight
 */
class CourseForm extends Course
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['teacher_id', 'price_status','is_serialize', 'apply_num', 'recruit_num', 'type_id', 'source_id', 'teacher_from_id', 'rank_id', 'cate_id', 'start_time', 'end_time', 'status', 'add_time', 'upd_time', 'weight'], 'integer'],
            [['course_price'], 'number'],
            [['course_name','user_name'], 'string', 'max' => 100],
            [['course_thumb'], 'file','extensions' => 'jpg, gif, png,pneg'],
            [['describe', 'fit_crowd', 'result'], 'string', 'max' => 255],
			
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'course_id' => 'Course ID',
            'course_name' => 'Course Name',
            'course_thumb' => 'Course Thumb',
            'teacher_id' => 'Teacher ID',
            'price_status' => 'Price Status',
            'course_price' => 'Course Price',
            'apply_num' => 'Apply Num',
            'recruit_num' => 'Recruit Num',
            'type_id' => 'Type ID',
            'source_id' => 'Source ID',
            'teacher_from_id' => 'Teacher From ID',
            'rank_id' => 'Rank ID',
            'cate_id' => 'Cate ID',
            'describe' => 'Describe',
            'fit_crowd' => 'Fit Crowd',
            'result' => 'Result',
            'start_time' => 'Start Time',
            'end_time' => 'End Time',
            'status' => 'Status',
            'add_time' => 'Add Time',
            'upd_time' => 'Upd Time',
            'weight' => 'Weight',
            'is_serialize'=> 'serialize',
			'user_name'=> 'UserName'
        ];
    }


    //课程与课程类型关系
    public function getCategoty()
    {
        return $this->hasOne(CourseCategoty::className(),['cate_id'=>'cate_id']);
    }
    //课程与课程分类关系
    public function getType()
    {
        return $this->hasOne(CourseType::className(),['type_id'=>'type_id']);

    }
    //课程与等级关系
    public function getRank()
    {
        return $this->hasOne(CourseRank::className(),['rank_id'=>'rank_id']);
    }
    //课程与来源关系
    public function getSource()
    {
        return $this->hasOne(CourseSource::className(),['source_id'=>'source_id']);
    }
     //课程与教师关系
    public function getTeacher()
    {
        return $this->hasOne(Teacher::className(),['teacher_id'=>'teacher_id']);
    }
	//查询课程
	public function CourseSel($course_ids)
    {
		return $this->find()
				->where("course_id in ($course_ids)")
				->asArray()
				->all();
	}
	//修改图片
	public function CourseUpdateImg($course_id,$rootPath)
    {
		return $this->updateAll(['course_thumb' => $rootPath],'course_id=:course',[':course'=>$course_id]);
	}
	/**
	* 查询条数
	*/
	public function CourseCount($code)
    {
		return $this->find()
					->where($code)
					->count();
	}
	/**
	* 搜索查询
	*/
	public function CourseSearch($code,$pagination)
    {
		return $this->find()
				->where($code)
				->orderBy(['add_time'=>SORT_DESC])
				->offset($pagination->offset)
				->limit($pagination->limit)
				->asArray()
				->all();
	}
}
